package com.minyon_web_admin.common.base;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.minyon_utils.StringUtil;
import com.minyon_web_admin.common.bean.AjaxResponseBean;
import com.minyon_web_admin.common.bean.DataTablesBean;
import com.minyon_web_admin.common.bean.MsgItem;
import com.minyon_web_admin.common.bean.ResBean;
import com.minyon_web_admin.common.bean.ResCode;
import com.minyon_web_admin.common.page.PageUtil;
import com.minyon_web_admin.shiro.bean.LoginUser;

/**
 * 控制器支持类
 * 
 * @author WWY
 * @version 2013-3-23
 */
public class BaseController {
	protected final Logger minYonlogger = (Logger) LoggerFactory
			.getLogger(getClass());// LoggerFactory.getLogger(LogbackConfigListener.class);

	/**
	 * 获取服务器返回的数据.
	 * 
	 * @param mp
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getResultMap(Map<String, Object> resultMap,boolean ifPage) {
				//普通请求
				if (resultMap.containsKey("RESULT")) {
					return (T) resultMap.get("RESULT");
				} else {
					minYonlogger.error("服务器返回的数据为空或格式不正确");
					return null;
				}
	}
	/**
	 * 处理服务返回的ajax请求数据结果.
	 * @param resultMap
	 * @param ifPage
	 * @return
	 */
	public AjaxResponseBean getAjaxResultMap(Map<String, Object> resultMap,boolean ifPage) {
		AjaxResponseBean ajaxResult=new AjaxResponseBean();
		ResBean resBean = (ResBean) resultMap.get(ResCode.RESMSGKEY);
		 //处理ajax请求失败
		if (!ResCode.SUCCESS.equals(resBean.getResCode())) {
			ajaxResult.setSuccess(false);
			ajaxResult.addErrorMessage(MsgItem.newMessageItem(resBean
					.getResMsg()));
		} else {
			ajaxResult.setSuccess(true);
			//如果是分页ajax请求
			if(ifPage){
				PageUtil<Object> page = (PageUtil<Object>) resultMap.get("RESULT");
				List<Object> resultList = page.getResult();
				ajaxResult.setData(resultList);
				// 分页代码
				ajaxResult.setPageStr(page.getPageStr());
				// 当前页码
				ajaxResult.setCurrentPage(page.getCurrentPage());
				// 总页码
				ajaxResult.setTotalPage(page.getTotalPage());
				// 总记录数
				ajaxResult.setTotalCount(page.getTotal());
			}else{
				ajaxResult.setData(resultMap.get("RESULT"));
			}
		}
		return ajaxResult;
	}
	
	/**
	 * 处理服务返回的ajax请求数据结果.
	 * @param resultMap
	 * @param ifPage
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public DataTablesBean<Object> getAjaxDataTablesBeanResultMap(Map<String, Object> resultMap) {
		DataTablesBean<Object> bean=new DataTablesBean<Object>();
		ResBean resBean = (ResBean) resultMap.get(ResCode.RESMSGKEY);
		 //处理ajax请求失败
		if (!ResCode.SUCCESS.equals(resBean.getResCode())) {
			bean.setSuccess(false);
			bean.setData(null);
			bean.setMsg(resBean.getResMsg());
		} else {
			bean.setSuccess(true);
			//如果是分页ajax请求
				PageUtil<Object> page = (PageUtil<Object>) resultMap.get("RESULT");
				List<Object> resultList = page.getResult();
				bean.setData(resultList);
				bean.setiTotalDisplayRecords(page.getTotal());
				bean.setiTotalRecords(page.getTotal());
				 bean.setsEcho(page.getsEcho());
				bean.setDraw(page.getDraw());
		}
		return bean;
	}
	
	
	/**
	 * 获取当前请求的页码.
	 * 
	 * @param req
	 * @return
	 */
	protected String getCurPage(HttpServletRequest req) {
		String currentPage = req.getParameter("currentPage");
		if (StringUtil.isEmpty(currentPage)) {
			return "1";
		}
		return currentPage;
	}
	
	/**
	 * 获取当前登录用户.
	 * 
	 * @param  
	 * @return
	 *  LoginUser
	 */
	protected  LoginUser getLoginUser() {
		Subject subject=SecurityUtils.getSubject();
		LoginUser currentUser=null;
		try{
		  currentUser=(LoginUser) subject.getSession().getAttribute("currentUser");
		}catch(InvalidSessionException ex){
			minYonlogger.error("当前缓存登录用户为空");
			return null;
		}
	     return currentUser;
	}
}
